package
{
	//import swhx
	import swhx.Deserializer;
	import swhx.Serializer;
	import swhx.Api;
	//import flash
	import flash.display.Sprite;
	import flash.text.*;
	
	import com.freesome.flii.Flii;
	
	public class FliiApp extends Sprite
	{
		private static var me: FliiApp = null;		
		private var log: TextField;		
		public var gui: Sprite;
		public var flii:Flii;
		
		public static function instance() : FliiApp {
			return me;
		}
		
		public function FliiApp() {
			if (me == null) { 
				me = this;			
				init();
			}
		}
			
		private function init() {			
			// Initialize the Screenmeaver HX binding.
			// the 'me' argument specifies the base object swhx
			// will use on trying to resolve external calls from
			// the haXe back-end.		
			swhx.Api.init(me);				
			log = new TextField();
			log.width=300;
			
			log.text="oiop";
			this.addChild(log);			
			
			// call haxe:
			callHaxe();
			var string: String = "Hello world !\nA \"quoted\" string and <some> &lt;html&gt;\r\n\t \\n \\\\r and special chars.";
			callHaxe_StringTest(string);
			callHaxe_StringTest('<a stf="&lt;asd"/>');
		}
		
		public function hello(x: Number, y: Number) : String {
			var result = "hello "+ (x + y);
			log.appendText( "--> haXe is calling function Hello("+x+","+y+") on Flash:\n--> Flash responds with:\n" );
			log.appendText( result + "\n" );
			return result;
		}
		
		public function callHaxe() {
			// Invoke a method on object 'obj' that lives in our haXe back-end,
			// and display the result:
			log.appendText( "--> Flash is calling function backend.hello(10,20) on haXe:\n-->haXe responds with: " );
			log.appendText( "" + swhx.Api.call("backend.hello",10,20)  );
			log.appendText( "\n" );
		}
		
		public function callHaxe_StringTest(string: String) {
			// Invoke another method on object 'obj':			
			log.appendText( "--> Flash is calling function backend.stringTest on haXe with argument:\n"+string);
			log.appendText( "\n--> haXe responds with:\n");
			log.appendText( "" + swhx.Api.call("backend.stringTest",string) );
			log.appendText( "\n" );
		}
	}
}